/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.sx.sx1.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.*;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.sx.sx1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Administrator
 */
@RestController
public class IndexController {

    @Autowired
    UserService userService;


    private static List<byte[]> CACHE = new ArrayList<>(1000);



    @RequestMapping("/index")
    // @ResponseBody
    public String firstController() {
        System.out.println(userService.getTest1Data());
        userService.add();
        return "hello springboot  i am running  in netbeans:"+ userService.getTest1Data();
    }

    //https://www.jb51.net/program/287389gy2.htm
    /**
     * 死循环 cpu 100%
     *
     * @return
     */
    @RequestMapping("/deadCycle")
    public String deadCycle() {
        for (int i = 1; i > 0; i++) {
            System.out.println(i);
        }
        return "Success";
    }

    /**
     * 死锁
     *
     * @return
     */
    @RequestMapping("/deadLock")
    public String deadLock() {
        Lock lock1 = new ReentrantLock();
        //线程t1中
        new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (!lock1.tryLock()) {
                System.out.println("线程1 一直自旋获取锁");
            }
            System.out.println("线程1 获取到锁 do something");
        }).start();
        // 线程2
        new Thread(() -> {
            lock1.lock();  //此时线程2 获取到锁 但是一直阻塞 导致锁无法释放
            while (true) {
            }
        }).start();
        return "Success";
    }
}
